<template>
  <div class="serach-result">
    <!-- 导航栏 $route.query.keyword获取路由参数-->
    <van-nav-bar
      :title="$route.query.keyword+'的搜索结果'"
      left-arrow
      fixed
      @click-left="$router.back()"
    />
    <!-- /导航栏 -->

    <!-- 文章列表
    van-list显示内容不足一屏自动调用onLoad
    -->
    <van-list
      class="article-list"
      v-model="loading"
      :finished="finished"
      finished-text="没有更多了"
      @load="onLoad"
    >
      <van-cell
        v-for="(item, idx) in list"
        :key="idx"
        :title="item.title"
        @click="$router.push('/article/' + item.art_id.toString())"
      />
    </van-list>
    <!-- /文章列表 -->
  </div>
</template>

<script>
import { getSearch } from '@/api/search.js';
export default {
  name: 'SearchResult',
  data () {
    return {
      list: [],
      loading: false,
      finished: false,
      page: 1 // 当前查询的页码
    };
  },

  methods: {
    async onLoad () {
      // 异步更新数据
      const result = await getSearch(this.$route.query.keyword, this.page);
      console.log('搜索结果', result);
      this.list.push(...result.data.data.results);
      this.loading = false;
      // 下次再查就是第二页
      this.page++;
      if (result.data.data.results.length === 0) {
        this.finished = true;
      }
    }
  }
};
</script>

<style lang="less" scoped>
.serach-result {
  height: 100%;
  overflow: auto;
  .article-list {
    margin-top: 39px;
  }
}
</style>
